# Contributor: Michał Polański <michal@polanski.me>
# Maintainer: Michał Polański <michal@polanski.me>
pkgname=containers-common
pkgver=0.48.0
pkgrel=0
pkgdesc="Configuration files for container tools"
url="https://github.com/containers/common"
license="Apache-2.0"
arch="noarch"
options="!check" # no test suite
makedepends="go-md2man"
subpackages="$pkgname-doc"
# Pick the newest versions of common/storage/image vendored
# in podman, skopeo and buildah (but there is no strict rule).
# Check them with the list_vendors function. Ideally, they
# should be the same.
_common_ver=$pkgver
_storage_ver=1.40.2
_image_ver=5.21.1
_podman_ver=4.1.0
_skopeo_ver=1.7.0
_buildah_ver=1.26.1
source="https://github.com/containers/common/archive/v$_common_ver/common-$_common_ver.tar.gz
	https://github.com/containers/storage/archive/v$_storage_ver/storage-$_storage_ver.tar.gz
	https://github.com/containers/image/archive/v$_image_ver/image-$_image_ver.tar.gz
	https://github.com/containers/podman/archive/v$_podman_ver/podman-$_podman_ver.tar.gz
	https://github.com/containers/skopeo/archive/v$_skopeo_ver/skopeo-$_skopeo_ver.tar.gz
	https://github.com/containers/buildah/archive/v$_buildah_ver/buildah-$_buildah_ver.tar.gz
	"

list_vendors() {
	unpack

	for tool in podman-$_podman_ver skopeo-$_skopeo_ver buildah-$_buildah_ver
	do
		cd "$srcdir"/$tool
		msg $tool
		grep github.com/containers/common go.mod
		grep github.com/containers/storage go.mod
		grep github.com/containers/image go.mod
	done
}

prepare() {
	default_prepare

	# fix go-md2man path in containers/storage
	sed -E 's/(GOMD2MAN =).*/\1 go-md2man/' -i "$srcdir"/storage-$_storage_ver/docs/Makefile

	# set default storage driver
	sed -E 's/(driver =) ""/\1 "overlay"/' -i "$srcdir"/storage-$_storage_ver/storage.conf

	# set unqualified-search-registries
	sed -E 's/# (unqualified-search-registries =).*/\1 ["docker.io"]/' -i "$srcdir"/image-$_image_ver/registries.conf
}

build() {
	cd "$srcdir"/common-$_common_ver
	make -C docs

	cd "$srcdir"/storage-$_storage_ver
	make -C docs

	cd "$srcdir"/image-$_image_ver
	make docs

	cd "$srcdir"/podman-$_podman_ver
	go-md2man -in pkg/hooks/docs/oci-hooks.5.md -out pkg/hooks/docs/oci-hooks.5
}

package() {
	install -d "$pkgdir"/etc/containers/certs.d
	install -d "$pkgdir"/etc/containers/oci/hooks.d
	install -d "$pkgdir"/var/lib/containers/sigstore

	cd "$srcdir"/common-$_common_ver
	install -Dm644 pkg/config/containers.conf "$pkgdir"/etc/containers/containers.conf
	install -Dm644 pkg/config/containers.conf "$pkgdir"/usr/share/containers/containers.conf
	install -Dm644 pkg/seccomp/seccomp.json "$pkgdir"/etc/containers/seccomp.json
	install -Dm644 pkg/seccomp/seccomp.json "$pkgdir"/usr/share/containers/seccomp.json
	make -C docs install PREFIX=/usr DESTDIR="$pkgdir"

	cd "$srcdir"/storage-$_storage_ver
	install -Dm644 storage.conf "$pkgdir"/etc/containers/storage.conf
	install -Dm644 storage.conf "$pkgdir"/usr/share/containers/storage.conf
	make -C docs install DESTDIR="$pkgdir"

	cd "$srcdir"/image-$_image_ver
	install -Dm644 registries.conf "$pkgdir"/etc/containers/registries.conf
	make install DESTDIR="$pkgdir"

	cd "$srcdir"/podman-$_podman_ver
	install -Dm644 pkg/hooks/docs/oci-hooks.5 "pkgdir"/usr/share/man/man5/oci-hooks.5

	cd "$srcdir"/skopeo-$_skopeo_ver
	install -Dm644 default-policy.json "$pkgdir"/etc/containers/policy.json
	install -Dm644 default.yaml "$pkgdir"/etc/containers/registries.d/default.yaml
}

doc() {
	default_doc
	pkgdesc="Man pages for container tools"
}

sha512sums="
82d823899a39e6d08de5e8880edc5af41165188d9a54346b0ddb0a27e3697af3a4b33e9905fa87ae9e545a0bb5f68fea9a88ae2268e252f0fe20852a41e80057  common-0.48.0.tar.gz
f4ff23e26d9103debc1709cbbbc6e715d2b9378a3bd7c2e72393841b2eb499ffce525526aa53bd1f7985cda87962a98f1d0b4601610f7cee8c9c879957575ad2  storage-1.40.2.tar.gz
d4d6d6dc9e1359dbf23165d0f636eeb89246efa89c2b38cb5760e3cec6439234fb708a72e8ec5d07fe8bf9a12ef81b9040c03d70cacff2764c926a3f77d133b0  image-5.21.1.tar.gz
4d5f240423f21b1224ce44853a2265359dacee0d921f0ea53a7fca34f9076ebfaa49f4327a59ac51f5bf5b9dafb47dda3a9bb8e609237cc69f833b42b95bcd0a  podman-4.1.0.tar.gz
7d17f98e86677150fb2ddba9edae18d8b1ac8f06fba575268e6ba54c60f7b371b5e5a9eab1a4fe19da6c7e85eb79c1eef169ad2a39eff979130ee8d141bd8d6b  skopeo-1.7.0.tar.gz
07225ebb23e5a815be1ef8acb5d15ef9fb8c6e9324fa9b65ec024f7b6bbe892721f9feaac2911d1b466c2cf336bd0c68e3d1b4df953f13a3390da84a93f76723  buildah-1.26.1.tar.gz
"
